// pages/register/register.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    region: ['广东省', '广州市', '天河区'],
    disabled: true,
    agentName: '',
    //顾问编号
    adviserNum: '',
    getPhoneNumberChange: false,
    //邀请信息
    enterpriseId: app.globalData.enterpriseId,
    userId: app.globalData.userId,
    refId: app.globalData.refId
  },

  toProtocol() {
    wx.navigateTo({
      url: '/pages/welcome/protocol/protocol'
    })
  },

  //选中协议
  checkChange(e) {
    this.setData({
      'disabled': e.detail.value.length === 0
    });
  },

  //姓名输入
  nameChange(e) {
    this.setData({
      "agentName": e.detail.value
    });
  },

  adviserNumChange(e){
    this.setData({
      "adviserNum": e.detail.value
    })
  },

  submit() {
    if (this.data.disabled) {
      wx.showToast({
        title: '请勾选注册协议',
        icon: 'none'
      });
    } else {
      this.register();
    }
  },

  //注册提交
  register() {
    var that = this;

    if (!this.data.agentName) {
      wx.showToast({
        "title": "请输入姓名",
        "icon": "none"
      });
      return;
    }
    wx.showLoading({
      "title": "拼命请求中...",
      "mask": true
    });
    var data;
    //邀请注册
    if (app.globalData.enterpriseId && app.globalData.idNumber && app.globalData.refId) {
      data = {
        "phone": this.data.phone,
        "agentName": this.data.agentName,
        "province": this.data.region[0],
        "city": this.data.region[1],
        "area": this.data.region[2],
        "enterpriseId": app.globalData.enterpriseId,
        "id_number": app.globalData.idNumber,
        "refId": app.globalData.refId,
        "union_id": wx.getStorageSync('union_id')
      }
    } else {
      //游客注册
      data = {
        "phone": this.data.phone,
        "agentName": this.data.agentName,
        "province": this.data.region[0],
        "city": this.data.region[1],
        "area": this.data.region[2],
        "id_number": this.data.adviserNum,
        "union_id": wx.getStorageSync('union_id')
      }
    }
    wx.request({
      url: app.globalData.url + "c=user&m=register",
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: data,
      success: res => {
        wx.hideLoading();
        res = res.data;
        if (res.status === 200) {
          wx.setStorageSync('user', res.data);
          wx.setStorageSync('user_id', res.data.id);
          //注册成功后，上传头像和手机号码
          wx.showLoading({
            "title": "拼命请求中...",
            "mask": true
          });
          wx.request({
            url: app.globalData.url + 'c=user&m=keepWeChat',
            data: {
              user_id: res.data.id,
              wx_head: wx.getStorageSync('wx_head'),
              wx_name: wx.getStorageSync('wx_name')
            },
            success: res => {
              wx.hideLoading();
              res = res.data;
              if (res.status == 200) {
                wx.reLaunch({
                  url: '/pages/welcome/certificate/certificate?name=' + that.data.agentName,
                });
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            }
          })
        } else {
          wx.showToast({
            "title": res.msg,
            "icon": "none"
          });
        }
      }
    });
  },

  //手动修改位置
  locationChange(e) {
    this.setData({
      'region': e.detail.value
    });
  },


  //获取手机号码
  getPhoneNumber(e) {
    if (e.detail.iv && e.detail.encryptedData) {
      this.setData({
        'getPhoneNumberChange': false
      });
      wx.showLoading({
        title: '拼命加载中...',
      });
      wx.request({
        url: `${app.globalData.url}c=home&m=getPhoneNumber`,
        data: {
          'encryptedData': e.detail.encryptedData,
          'iv': e.detail.iv,
          'code': wx.getStorageSync('code')
        },
        success: res => {
          wx.hideLoading();
          res = res.data;
          if (res.status == 200) {
            wx.setStorageSync('phone', res.data.purePhoneNumber);
            this.setData({
              'phone': res.data.purePhoneNumber
            });
            wx.showToast({
              title: '获取手机号码成功',
              icon: 'none'
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
            this.setData({
              getPhoneNumberChange: true
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '授权失败，请允许',
        icon: 'none'
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var phone = wx.getStorageSync('phone');
    this.setData({
      adviserNum: app.globalData.idNumber
    })
    if (!phone) {
      //获取手机号码需要解析
      wx.login({
        success: res => {
          wx.setStorageSync('code', res.code);
          this.setData({
            'getPhoneNumberChange': true
          })
        }
      })
    } else {
      this.setData({
        'phone': phone
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})